package com.labyrinth.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

import static com.badlogic.gdx.graphics.GL10.GL_TRIANGLE_STRIP;

public class GameInterface
{
    //Mesh
    Mesh mesh;
    //Int
    public int moveCount = 0;
    private int startMove = 0;
    public int mazeNR = 1;
    int drawType;
    int vertexCount;
    public int start = 1;
    public int last = 1;
    public int startT = 0;
    private int maxLVL = 0;
    private int currentPage = 1;
    private int infoPage = 1;
    public boolean LevelStart = false;


    //Float
    float scalex;
    float scaley;
    public float bleh = 1;
    //Clock
    private Clock clock1;
    private Clock clock2;
    private Clock clock3;
    private Clock Move1;
    private Clock Move2;
    private Clock Move3;
    private Clock LvNr1;
    private Clock LvNr2;
    private Clock LvNr3;
    //button
    private Button Background;
    private Button InfoScreen;
    private Button StoryScreen;
    private Button newGame;
    private Button info;
    private Button MazeNr;
    public Button MenuB;
    private Button SelectLVL;
    private Button Exit;
    private Button Next;
    private Button Previous;
    private Button Continue;
    private Button Winner;
    //levelX
    private levelX levelSelect1;
    private levelX levelSelect2;
    private levelX levelSelect3;
    private levelX levelSelect4;
    //other

    Texture texture;
    public Vector2D position;
    public boolean Menu = true;
    public boolean WinnerScreen = false;
    public boolean infiniteMove = false;
    public String Screen = "Menu";
    private final String TextureFolder = "data/button/test_font/";
    private final String preferedType = ".png";


    GameInterface()
    {

    }

    public GameInterface(float x, float y)
    {
        position = new Vector2D(x, y);
        Screen = "Menu";
        int buttonScale=70;this.setScale(buttonScale, buttonScale);
        setTexture(TextureFolder+"buttonReset"+preferedType);
        this.setScale(buttonScale);
        setDrawType(GL_TRIANGLE_STRIP);
        setMesh();
        MenuB = new Button(x + 100,y,TextureFolder+"buttonMenu"+preferedType);
        MenuB.setScale(40,65);
        this.setScale(buttonScale, buttonScale);
        this.setScale(buttonScale);
        setDrawType(GL_TRIANGLE_STRIP);
        setMesh();

        clock1 = new Clock(x - 240, y + 20, 1);
        clock2 = new Clock(x - 240, y, 2);
        clock3 = new Clock(x - 240, y - 20, 3);

        Move1 = new Clock(x - 200, y + 20, 1);
        Move2 = new Clock(x - 200, y, 2);
        Move3 = new Clock(x - 200, y - 20, 3);

        Background = new Button(240,425,"data/button/Labyrinth.jpg");
        Background.setScale(490,850);
        newGame = new Button(100,150,TextureFolder+"buttonNewGame.png");
        newGame.setScale(70,150);
        info = new Button(180,150,TextureFolder+"buttonInfo"+preferedType);
        InfoScreen = new Button(250,500,TextureFolder+"InfoScreen1"+preferedType);
        InfoScreen.setScale(400,600);
        StoryScreen = new Button(250,500,TextureFolder+"Story"+preferedType);
        StoryScreen.setScale(400,600);

        Winner = new Button(250,500,TextureFolder+"Winner"+preferedType);
        Winner.setScale(300,500);

        Continue = new Button(260,150,TextureFolder+"Continue"+preferedType);
        Next = new Button(260,150,TextureFolder+"buttonNext"+preferedType);
        Previous = new Button(340,150,TextureFolder+"buttonPrevious"+preferedType);
        Previous.setScale(60,100);
        Exit = new Button(420,150,TextureFolder+"buttonexit2"+preferedType);
        SelectLVL = new Button(260,150,TextureFolder+"buttonSelect"+preferedType);
        MazeNr = new Button(180,150,TextureFolder+"buttonMazeNR"+preferedType);
        levelSelect1 = new levelX(150,350,TextureFolder+"buttonMazeNR"+preferedType);
        levelSelect2 = new levelX(220,350,TextureFolder+"buttonMazeNR"+preferedType);
        levelSelect3 = new levelX(290,350,TextureFolder+"buttonMazeNR"+preferedType);
        levelSelect4 = new levelX(360,350,TextureFolder+"buttonMazeNR"+preferedType);

        LvNr1 = new Clock(260, 220, 3);
        LvNr2 = new Clock(260, 240, 2);
        LvNr3 = new Clock(260, 260, 1);
    }

    public void setStartT(int StartT)
    {
        startT = StartT;

        if (this.startT != 0)
        {
            bleh = startT+1;
        }
    }

    public void setMaxLvL(int Max)
    {
        maxLVL = Max;
    }

    public int getStartT()
    {
        return startT;
    }

    public int getStartMove()
    {
        return startMove;
    }

    public void setStartMove(int StartMove)
    {
        startMove = StartMove;

        if (this.startMove != 0)
        {
            moveCount = startMove;
            infiniteMove = false;
        }
        else
        {
            Move1.timer = "data/time/Time0.png";
            infiniteMove = true;
            moveCount = 1;
        }
    }

    // up
    void setMesh()
    {
        mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
        mesh.setVertices(new float[] { -0.3f, -0.5f, 0, 0, 1, 0.3f, -0.5f, 0, 1, 1, -0.3f, 0.5f, 0, 0, 0, 0.3f, 0.5f, 0, 1, 0 });
        mesh.setIndices(new short[] { 0, 1, 2, 3 });
        vertexCount = 4;

    }

    public float getX()
    {
        return position.getX();
    }

    public float getY()
    {
        return position.getY();
    }

    void setDrawType(int drawType)
    {
        this.drawType = drawType;
    }

    public float getScalex()
    {
        return scalex;
    }

    public float getScaley()
    {
        return scaley;
    }

    void setTexture(String texturePath)
    {
        FileHandle textureFileHandle = Gdx.files.internal(texturePath);
        texture = new Texture(textureFileHandle);
    }

    Texture setTexture2(String texturePath, Texture texture2)
    {
        FileHandle textureFileHandle = Gdx.files.internal(texturePath);
        texture2 = new Texture(textureFileHandle);
        return texture2;
    }

    void setScale(float scalex, float scaley)
    {
        this.scalex = scalex;
        this.scaley = scaley;
    }

    void setScale(float scale)
    {
        this.scalex = scale;
        this.scaley = scale;
    }

    void Timer(int timer)
    {
        clock1.setClock(timer);
        clock2.setClock(timer);
        clock3.setClock(timer);
    }
    public void MazeNr(int timer)
    {
        LvNr1.setClock(timer);
        LvNr2.setClock(timer);
        LvNr3.setClock(timer);
    }

    public void Counter(int moves)
    {
        if (this.startMove != 0)
        {
            Move1.setClock(moves);
            Move2.setClock(moves);
            Move3.setClock(moves);
        }
    }

    void drawMenu()
    {
        Background.draw();
        newGame.draw();
        info.draw();
        SelectLVL.draw();
    }

    void drawStory()
    {
        Background.draw();
        StoryScreen.draw();
        Continue.draw();

    }

    void drawInfo()
    {
        Background.draw();
        info.draw();
        InfoScreen.draw();
        Next.draw();
        Previous.draw();
        Exit.draw();
    }

    void drawSelect()
    {
        Background.draw();
        MazeNr.draw();
        Next.draw();
        Previous.draw();
        Exit.draw();
        levelSelect1.SetNumber(currentPage);
        levelSelect1.drawX();
        if(currentPage+1 <= maxLVL)
        {
            levelSelect2.SetNumber(currentPage+1);
            levelSelect2.drawX();
        }
        if(currentPage+2 <= maxLVL)
        {
            levelSelect3.SetNumber(currentPage+2);
            levelSelect3.drawX();
        }
        if(currentPage+3 <= maxLVL)
        {
            levelSelect4.SetNumber(currentPage+3);
            levelSelect4.drawX();
        }


    }
    void drawWinner()
    {
        Background.draw();
        Winner.draw();

        Continue.draw();
    }

    void drawGame()
    {

        Gdx.gl10.glPushMatrix();
        Gdx.gl10.glTranslatef(position.getX(), position.getY(), 0);
        Gdx.gl10.glScalef(scalex, scaley, 0);
        Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
        texture.bind();
        mesh.render(drawType, 0, vertexCount);
        Gdx.gl10.glPopMatrix();
        
        MenuB.draw();
        clock1.draw();
        clock2.draw();
        clock3.draw();

        Move1.draw();
        Move2.draw();
        Move3.draw();

         if(LevelStart)
        {
            float deltaTime = Gdx.graphics.getDeltaTime();

            if (this.startT != 0)
            {
                this.Timer(start);

                start = (int) bleh;
                if (start != last)
                {
                    last = start;
                }
                bleh -= 1.0f * deltaTime;
            }
            else
            {
                start = 0;
                this.Timer(start);
            }
        }

    }

    public void draw()
    {
        if(Screen.equals("game"))
        {
            drawGame();
        }
        else if(Screen.equals("Info"))
        {
            drawInfo();
        }
        else if(Screen.equals("Menu"))
        {
            drawMenu();
        }
        else if(Screen.equals("Select"))
        {
            drawSelect();
        }
        else if(Screen.equals("Story"))
        {
            drawStory();
        }

        else if(Screen.equals("Winner"))
        {
            drawWinner();
        }
    }

    public void Clicked(float X,float Y)
    {
        boolean menuActive=true;if(Screen.equals("game"))
        {

        }
        else if(Screen.equals("Info"))
        {
            if(!Next.clicked(X,Y))
            {
                if(infoPage == 1)
                {
                    InfoScreen.setTexture(TextureFolder+"InfoScreen2"+preferedType);
                    infoPage = 2;
                }
                else
                {
                    InfoScreen.setTexture(TextureFolder+"InfoScreen1"+preferedType);
                    infoPage = 1;
                }
            }
            if(!Previous.clicked(X,Y))
            {
                if(infoPage == 1)
                {
                    InfoScreen.setTexture(TextureFolder+"InfoScreen2"+preferedType);
                    infoPage = 2;
                }
                else
                {
                    InfoScreen.setTexture(TextureFolder+"InfoScreen1"+preferedType);
                    infoPage = 1;
                }
            }
            if(!Exit.clicked(X,Y))
            {
                Screen = "Menu";
            }

        }
        else if(Screen.equals("Menu"))
        {
            if(!newGame.clicked(X,Y))
            {

                Screen = "Story";

            }

            if(!info.clicked(X,Y))
            {
                Screen= "Info";
            }

            if(!SelectLVL.clicked(X,Y))
            {
                Screen= "Select";
            }

        }
        else if(Screen.equals("Select"))
        {
            if(!levelSelect1.clickedX(X,Y))
            {
                Menu = false;
                mazeNR = levelSelect1.number;
                menuActive = false;
                Screen = "game";
            }

            if(!levelSelect2.clickedX(X,Y))
            {
                Menu = false;
                mazeNR = levelSelect2.number;
                menuActive = false;
                Screen = "game";
            }

            if(!levelSelect3.clickedX(X,Y))
            {
                Menu = false;
                mazeNR = levelSelect3.number;
                menuActive = false;
                Screen = "game";
            }

            if(!levelSelect4.clickedX(X,Y))
            {
                Menu = false;
                mazeNR = levelSelect4.number;
                menuActive = false;
                Screen = "game";
            }

            if(!Next.clicked(X,Y) && currentPage+4 <= maxLVL)
            {
                currentPage +=4;
            }

            if(!Previous.clicked(X,Y))
            {
                if(currentPage != 1)
                {
                    currentPage -=4;
                }
            }

            if(!Exit.clicked(X,Y))
            {
                Screen = "Menu";
            }


        }
        else if(Screen.equals("Story"))
        {
            if(!Continue.clicked(X,Y))
            {
                Menu = false;
                mazeNR = 1;
                menuActive = false;
                Screen = "game";
            }
        }
        else if(Screen.equals("Winner"))
        {
            if(!Continue.clicked(X,Y))
            {
                Menu = false;

                menuActive = false;
                this.Screen = "game";
            }
        }
    }
}